package com.aleaho.cftrack.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.R;
import com.aleaho.cftrack.activity.tree.HeaderViewHolder;
import com.aleaho.cftrack.activity.tree.IconTreeItemHolder;
import com.aleaho.cftrack.entity.DepartmentAndUser;
import com.aleaho.cftrack.http.RequestParam;
import com.aleaho.cftrack.http.entity.DepartmentTreeEntity;
import com.aleaho.cftrack.http.entity.LoginEntity;
import com.aleaho.cftrack.presenter.DepartmentTreePresenter;
import com.aleaho.cftrack.utils.Constants;
import com.aleaho.cftrack.utils.TrackConsts;
import com.aleaho.cftrack.utils.TreeUtil;
import com.aleaho.cftrack.utils.ViewUtil;
import com.aleaho.cftrack.view.DepartmentTreeView;
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import java.util.List;

import javax.security.auth.login.LoginException;

public class DepartmentTreeActivity extends Activity implements DepartmentTreeView {


    private DepartmentTreePresenter dtPresenter = null;


    private Intent result = null;
    private ViewGroup container = null;
    private LoadingDialog dialog;

    private String tracker = null;
    private String entityName = null;

    RequestParam param = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_department_tree);
        setDisplay();
        result = new Intent();

        dialog = new LoadingDialog(this);
        dialog.setCanceledOnTouchOutside(false);

        container = (ViewGroup) findViewById(R.id.container);

        dtPresenter = new DepartmentTreePresenter(this, this);

        getTreeNodeData();

        dialog.show();
        dialog.setMessage("正在加载数据....");
    }

    private int getDepartmentPK() {

        CFTrackApplication trackApp = (CFTrackApplication) getApplicationContext();

        LoginEntity le = trackApp.getLoginEntity();

        if (le == null) {
            return -1;
        } else {
            return trackApp.getLoginEntity().getLoginUser().getDepartment().getId();
        }
    }


    private void getTreeNodeData() {

        int departmentPK = getDepartmentPK();
        if (departmentPK < 1) {
            finish();
        }

        if (departmentPK == 1) {
            dtPresenter.getAllTreeNodeData();
        } else {
            dtPresenter.getSingleTreeNodeData(departmentPK);
        }
    }

    /**
     * 为获取屏幕宽、高
     */
    private void setDisplay() {

        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point point = new Point();

        d.getSize(point);

        p.width = (int) (point.x * 0.8);
        p.height = (int) (point.y * 0.8);

        getWindow().setAttributes(p);
    }


    @Override
    public void displayDepartmentTree(AndroidTreeView treeView) {

        treeView.setDefaultNodeLongClickListener(nodeLongClickListener);
        treeView.setDefaultNodeClickListener(nodeClickListener);

        container.addView(treeView.getView());
        dialog.dismiss();
    }

    /**
     * 提示错误信息
     *
     * @param message 错误信息
     */
    @Override
    public void showErrorMessage(String message) {
        dialog.dismiss();
        new ViewUtil().showToast(DepartmentTreeActivity.this, message);
    }


    public void selected() {

        result.putExtra(TrackConsts.TRACKER, tracker);

        result.putExtra(TrackConsts.ENTITY, entityName);

        setResult(Constants.TRACKER_RESULT_CODE, result);
        super.onBackPressed();
    }


    private TreeNode.TreeNodeClickListener nodeClickListener = new TreeNode.TreeNodeClickListener() {
        @Override
        public void onClick(TreeNode node, Object value) {
            IconTreeItemHolder.IconTreeItem item = (IconTreeItemHolder.IconTreeItem) value;

            if (1 == item.duEntity.flag) {
                tracker = item.duEntity.mName;
                entityName = item.duEntity.mUserName + "_" + tracker;
                selected();
            }
        }
    };

    private TreeNode.TreeNodeLongClickListener nodeLongClickListener = new TreeNode.TreeNodeLongClickListener() {
        @Override
        public boolean onLongClick(TreeNode node, Object value) {
            IconTreeItemHolder.IconTreeItem item = (IconTreeItemHolder.IconTreeItem) value;

            if (1 == item.duEntity.flag) {
                tracker = item.duEntity.mName;
                entityName = item.duEntity.mUserName + "_" + tracker;
                selected();
            }
            return true;
        }
    };


}
